<?php
class M_tag extends Module {
  public function main() {
    if ( isset(Site::$URL[3]) && ( strpos(Site::$URL[3],'page') === 0 ) ) {
      Site::$URL[3] = substr( Site::$URL[3], 4 );
      $pn = Site::$URL[3]+0;
    } else {
      $pn = 1;
    }
  
    $sql = "SELECT `tag` AS `id` FROM `tags_names` WHERE `permalink`=':1'";
    $id = db::q( $sql, Site::$URL[2] )->get('id');

    $list = new Pub_List();
    $list->page = $pn;
    $list->num = 10;
    
    $list->sql = "
         SELECT `posts`.`id`, `posts`.`title`, `posts`.`text`, `posts`.`date`, `posts`.`permalink`,
                `posts`.`type`, `posts`.`meta`
           FROM `posts`
         LEFT JOIN `tags_relat`
           ON ( `posts`.`id` = `tags_relat`.`post` )
         LEFT JOIN `tags_names`
           ON ( `tags_relat`.`tag` = `tags_names`.`id` )
         WHERE `tags_names`.`tag` = '$id'
         GROUP BY `posts`.`id`
         ORDER BY `date` DESC
         ";
  
    $list->load();
    $list->template = 'log';
    $tmp = $list->get_html();
    $list->sql = "
         SELECT * FROM `posts`
         LEFT JOIN `tags_relat`
           ON ( `posts`.`id` = `tags_relat`.`post` )
         WHERE `tags_relat`.`tag` = '$id'";
    return $tmp.$list->page_nav( '/log/page' ) ;
  }
}
?>